package cn.edu.hbwe.book.service.book;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;

import cn.edu.hbwe.book.dao.BookDao;
import cn.edu.hbwe.book.dao.impl.BookDaoImpl;
import cn.edu.hbwe.book.entity.Book;
import cn.edu.hbwe.book.entity.PageBean;
import cn.edu.hbwe.book.exception.FindProductByIdException;


public class BookService {
	//前台，获取本周热销商品
		public List<Object[]> getWeekHotProduct() {
			BookDaoImpl dao=new BookDaoImpl();
			try {
				return dao.getWeekHotProduct();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException("前台获取本周热销商品失败！");
			}
		}
	public List<Book> findAll() {
		BookDaoImpl bd=new BookDaoImpl();
		return bd.findAll();
	}
	public Book findProductById(String id) throws FindProductByIdException {
		BookDaoImpl dao = new BookDaoImpl();
		try {
			return dao.findProductById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new FindProductByIdException("根据ID查找商品失败");
		}
	}
	public PageBean findProductByPage(int currentPage, int currentCount,String category) {
		BookDaoImpl dao = new BookDaoImpl();
		PageBean bean = new PageBean();
		// 封装每页显示数据条数
		bean.setCurrentCount(currentCount);
		// 封装当前页码
		bean.setCurrentPage(currentPage);
		// 封装当前查找类别
		bean.setCategory(category);
		try {
			// 获取总条数
			int totalCount = dao.findAllCount(category);
			bean.setTotalCount(totalCount);
			// 获取总页数
			int totalPage = (int) Math.ceil(totalCount * 1.0 / currentCount);
			bean.setTotalPage(totalPage);
			// 获取当前页数据
			List<Book> ps = dao.findByPage(currentPage, currentCount,category);
			bean.setPs(ps);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bean;
	}
	public void deleteBook(String id) {
		BookDaoImpl bd=new BookDaoImpl();
		bd.deleteBook(id);
	}
	public List<Book> findBooksbyCondition(String id,String name,String category,String minprice,String maxprice) {
		BookDaoImpl bd=new BookDaoImpl();
		return bd.findBooksbyCondition(id, name, category, minprice, maxprice);
	}
	public void addbookList(Book book) {
		BookDaoImpl bd=new BookDaoImpl();
		bd.addbookList(book);
	}
	public void updateBookMessage(String id,Book book) {
		BookDaoImpl bd=new BookDaoImpl();
		bd.updateBookMessage(id, book);
	}
	public  Book Findone(String id){
		BookDaoImpl bd=new BookDaoImpl();
		return bd.Findone(id);
	}
	// 下载销售榜单
		public List<Object[]> dl(String year, String month) {
			BookDaoImpl dao = new BookDaoImpl();
			List<Object[]> salesList = null;
			try {
				salesList = dao.salesList(year, month);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return salesList;
		}
}
